var app = getApp()
//豆瓣各种电影类型
var doubanMovieType = app.globalData.g_doubanMovieType
//豆瓣搜索URL
var doubanSearchUrl =app.globalData.g_doubanSearchUrl
var scrollNum = app.globalData.g_scrollLoadNum
var util = require('../util/util.js')
Page({
    data:{
      inTheaters:{},
      comingSoon:{},
      top250:{},
      searchMovie:{},
      //控制电影列表的切换
      movieListFlag:true,
      //搜索开始的序号
      searchStartNum:0,
      //搜索信息
      searchText:"",
      //目前搜索信息的总条数
      totalNum:0
    },
    onLoad:function(){
      var that = this
      for (let i = 0; i < doubanMovieType.length; i++) {
        let dataName = doubanMovieType[i].name
        util.getDoubanMsg(doubanMovieType[i].url + "?count=3", function (data) {
          var movies = util.resolveDoubanMsg(data)
          //将豆瓣的电影信息绑定在data中
          var doubanData = {}
          doubanData[dataName] = {
            movies: movies,
            movieType: dataName,
            slogan: util.convertMovieType(dataName)
          }
          that.setData(doubanData)
        })
      }
      //获取正在上映的电影信息
      // this.getDoubanMsg(doubanMovieType.inTheaters.url+"?count=3","inTheaters")
      //获取即将上映的电影信息
      // this.getDoubanMsg(doubanMovieType.comingSoon.url+"?count=3","comingSoon")
      //获取top250电影
      // this.getDoubanMsg(doubanMovieType.top250.url+"?count=3","top250")
  },
  //获取豆瓣电影信息
  // getDoubanMsg:function(url,dataName){
  //   var that = this
  //   wx.request({
  //         url: url,
  //         method: 'GET',
  //         header: {
  //             'Content-Type':'json'
  //         }, // 设置请求的 header
  //         success: function(res){
  //           var movies = that.resolveDoubanMsg(res.data)
  //           //将豆瓣的电影信息绑定在data中
  //           var doubanData = {}
  //           doubanData[dataName] = {
  //             movies:movies,
  //             movieType:dataName,
  //             slogan:util.convertMovieType(dataName)
  //           }
  //           that.setData(doubanData)
  //         },
  //         fail: function() {
  //           // fail
  //         },
  //         complete: function() {
  //           // complete
  //         }
  //       })
  // },
  //处理豆瓣电影信息
  // resolveDoubanMsg:function(data){
  //   var movies = []
  //   var dataSubjects = data.subjects
  //   var that = this
  //   for(var i=0;i<dataSubjects.length;i++){
  //       var movieId = dataSubjects[i].id
  //       var title = dataSubjects[i].title
  //       var rating = dataSubjects[i].rating.average
  //       var image = dataSubjects[i].images.large
  //       movies.push({
  //         "movieId":movieId, 
  //         "title":title,
  //         "rating":that.resolveRating(rating),
  //         "image":image
  //       })
  //   }
  //   return movies 
  // },
  // //处理星星组件的方法
  // resolveRating:function(rating){
  //   //根据0.5取整
  //   var ratingNum = Math.floor(Math.round(rating)/2*2)/2
  //   var hasHalf = ratingNum%1==0?false:true
  //   var ratingFlag = parseInt(ratingNum)
  //   var ratingArr = []
  //   for(var i=1;i<=ratingFlag;i++){
  //     ratingArr.push('full')
  //   }
  //   if(hasHalf){
  //     ratingArr.push('half')
  //   }
  //   while(ratingArr.length<5){
  //     ratingArr.push('empty')
  //   }
  //   return {
  //       'rating':rating,
  //       'ratingArr':ratingArr
  //   }
  // },
  //跳转到更多页
  onMoreMovie:function(event){
    var movieType = event.currentTarget.dataset.movietype
    wx.navigateTo({
      url: 'movie-more/movie-more?movieType='+movieType,
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  //跳转到电影详情页
  forwardDetail:function(event){
    var movieId = event.currentTarget.dataset.id
        wx.navigateTo({
          url: 'movie-detail/movie-detail?id='+movieId,
          success: function(res){
            // success
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }
        })
  },
  //input的内容确认时
  searchConfirm: function (event) {
    var searchText = event.detail.value
    var that = this
    wx.showNavigationBarLoading()
    util.getDoubanMsg(doubanSearchUrl + searchText + '&start=0&count=' + scrollNum, function (data) {
      var totalNum = data.total
      var movies = util.resolveDoubanMsg(data)
      var doubanData = {}
      doubanData.searchMovie = {
        movies: movies
      }
      that.setData(doubanData)
      that.setData({
        searchText:searchText,
        searchStartNum:scrollNum,
        totalNum:totalNum
        
      })
      wx.hideNavigationBarLoading()
    })
  },
  //input获取焦点时时
  searchFocus:function(event){
    this.setData({
      movieListFlag:false
    })
  },
  //点击搜索上的关闭按钮时时
  closeSearch:function(){
    //将各种数据初始化
    this.setData({
      movieListFlag:true,
      searchMovie:{},
      searchStartNum:0,
      searchText:"",
      totalNum:0
    })
  },
  //滑动加载
  onReachBottom: function () {
    var that = this
    var movieListFlag = this.data.movieListFlag
    var searchText = this.data.searchText
    var movies = this.data.searchMovie.movies
    var totalNum = this.data.totalNum
    var searchStartNum = this.data.searchStartNum
    if (!movieListFlag && movies && searchStartNum < totalNum - 1) {
      wx.showNavigationBarLoading()
      util.getDoubanMsg(doubanSearchUrl + searchText + '&start=' + searchStartNum + '&count=' + scrollNum, function (data) {
        var new_movies = util.resolveDoubanMsg(data)
        movies = movies.concat(new_movies)
        var doubanData = {}
        doubanData.searchMovie = {
          movies: movies
        }
        that.setData(doubanData)
        that.setData({
          searchStartNum:searchStartNum+scrollNum
        })
        wx.hideNavigationBarLoading()
      })
    }
  }
})